home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtoyc01.zip / HELPBASE.CPP < prev    next >
C/C++ Source or Header  |  1994-01-07  |  16KB  |  711 lines

  1. /*------------------------------------------------------------*/
  2. /* filename -       HelpBase.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  Member function(s) of following classes   */
  6. /*                      THelpTopic                            */
  7. /*                      THelpIndex                            */
  8. /*                      THelpFile                             */
  9. /*------------------------------------------------------------*/
  10.  
  11. /*------------------------------------------------------------*/
  12. /*                                                            */
  13. /*    Turbo Vision -  Version 1.0                             */
  14. /*                                                            */
  15. /*                                                            */
  16. /*    Copyright (c) 1991 by Borland International             */
  17. /*    All Rights Reserved.                                    */
  18. /*                                                            */
  19. /*------------------------------------------------------------*/
  20.  
  21. #define Uses_TStreamableClass
  22. #define Uses_TPoint
  23. #define Uses_TStreamable
  24. #define Uses_ipstream
  25. #define Uses_opstream
  26. #define Uses_fpstream
  27. #define Uses_TRect
  28. #include <tv.h>
  29.  
  30. #if !defined( __HELP_H )
  31. #include "HelpBase.h"
  32. #endif  // __HELP_H
  33.  
  34. #if !defined( __UTIL_H )
  35. #include "Util.h"
  36. #endif  // __UTIL_H
  37.  
  38. #if !defined( __STRING_H )
  39. #include <string.h>
  40. #endif  // __STRING_H
  41.  
  42. #if !defined( __LIMITS_H )
  43. #include <limits.h>
  44. #endif  // __LIMITS_H
  45.  
  46. #if !defined( __STAT_H )
  47. #include <sys\stat.h>
  48. #endif  // __STAT_H
  49.  
  50. #if !defined( __CTYPE_H )
  51. #include <ctype.h>
  52. #endif  // __CTYPE_H
  53.  
  54. #if !defined( __IO_H )
  55. #include <io.h>
  56. #endif  // __IO_H
  57.  
  58. #pragma warn -dsz
  59.  
  60. #ifdef HELPEXTENSIONS
  61.     int THelpFile::oldFront;
  62.     int THelpFile::oldCount;
  63.     ushort THelpFile::oldTopics[THelpFile::maxOldTopics];
  64. #endif
  65.  
  66.  
  67. TCrossRefHandler crossRefHandler = notAssigned;
  68.  
  69. // THelpTopic
  70. const char * const near THelpTopic::name = "THelpTopic";
  71.  
  72. void THelpTopic::write( opstream& os )
  73. {
  74.     writeParagraphs( os );
  75.     writeCrossRefs( os );
  76.     
  77. }
  78.  
  79. void *THelpTopic::read( ipstream& is )
  80. {
  81.     readParagraphs( is );
  82.     readCrossRefs( is );
  83.     width = 0;
  84.     lastLine = INT_MAX; 
  85.     return this;
  86. }
  87.  
  88. TStreamable *THelpTopic::build()
  89. {
  90.     return new THelpTopic( streamableInit );
  91. }
  92.  
  93.  
  94. TStreamableClass RHelpTopic( THelpTopic::name,
  95.                                   THelpTopic::build,
  96.                                   __DELTA(THelpTopic)
  97.                                 );
  98.  
  99. THelpTopic::THelpTopic() : TObject()
  100. {
  101.     paragraphs = 0;
  102.     numRefs = 0;
  103.     crossRefs = 0;
  104.     width = 0;
  105.     lastOffset = 0;
  106.     lastLine = INT_MAX;
  107.     lastParagraph = 0;
  108. };
  109.  
  110. void THelpTopic::readParagraphs( ipstream& s )
  111. {
  112.     int  i, size;
  113.     TParagraph **pp;
  114.     int temp;
  115.  
  116.     s >> i;
  117.     pp = ¶graphs;
  118.     while ( i > 0)
  119.     {
  120.         s >> size;
  121.         *pp = new TParagraph;
  122.         (*pp)->text = new char[size];
  123.         (*pp)->size = (ushort) size;
  124.     s >> temp;
  125.         (*pp)->wrap = Boolean(temp);
  126.         s.readBytes((*pp)->text, (*pp)->size);
  127.         pp = &((*pp)->next);
  128.         --i;
  129.     }
  130.     *pp = 0;
  131. }
  132.  
  133. void THelpTopic::readCrossRefs( ipstream& s )
  134. {
  135.     int i;
  136.     TCrossRef *crossRefPtr;
  137.  
  138.     s >> numRefs;
  139.     crossRefs = new TCrossRef[numRefs];
  140.     for (i = 0; i < numRefs; ++i)
  141.         {
  142.         crossRefPtr = (TCrossRef *)crossRefs + i;
  143.         s.readBytes(crossRefPtr, sizeof(TCrossRef));
  144.         }
  145. }
  146.  
  147. void THelpTopic::disposeParagraphs()
  148. {
  149.     TParagraph *p, *t;
  150.  
  151.     p = paragraphs;
  152.     while (p != 0)
  153.         {
  154.         t = p;
  155.         p = p->next;
  156.         delete t->text; 
  157.         delete t;
  158.         }
  159. }
  160.  
  161.  
  162. THelpTopic::~THelpTopic()
  163. {
  164.     TCrossRef *crossRefPtr;
  165.  
  166.     disposeParagraphs();
  167.     if (crossRefs != 0)
  168.        {
  169.        crossRefPtr = (TCrossRef *)crossRefs;
  170.        delete [numRefs] crossRefPtr;
  171.        }
  172. }
  173.  
  174. void THelpTopic::addCrossRef( TCrossRef ref )
  175. {
  176.     TCrossRef *p;
  177.     TCrossRef *crossRefPtr;
  178.  
  179.     p =  new TCrossRef[numRefs+1];
  180.     if (numRefs > 0)
  181.         {
  182.         crossRefPtr = crossRefs;
  183.         memmove(p, crossRefPtr, numRefs * sizeof(TCrossRef));
  184.         delete [numRefs] crossRefPtr;
  185.         }
  186.     crossRefs = p;
  187.     crossRefPtr = crossRefs + numRefs;
  188.     *crossRefPtr = ref;
  189.     ++numRefs;
  190. }
  191.  
  192.  
  193. void THelpTopic::addParagraph( TParagraph *p )
  194. {
  195.     TParagraph  *pp, *back;
  196.  
  197.     if (paragraphs == 0)
  198.         paragraphs = p;
  199.     else
  200.         {
  201.         pp = paragraphs;
  202.         back = pp;
  203.         while (pp != 0)
  204.             {
  205.             back = pp;
  206.             pp = pp->next;
  207.             }
  208.         back->next = p;
  209.         }
  210.     p->next = 0;
  211. }
  212.  
  213. void THelpTopic::getCrossRef( int i, TPoint& loc, uchar& length,
  214.          int& ref )
  215. {
  216.     int oldOffset, curOffset, offset, paraOffset;
  217.     TParagraph *p;
  218.     int line;
  219.     TCrossRef *crossRefPtr;
  220.  
  221.     paraOffset = 0;
  222.     curOffset = 0;
  223.     oldOffset = 0;
  224.     line = 0;
  225.     crossRefPtr = crossRefs + i;
  226.     offset = crossRefPtr->offset;
  227.     p = paragraphs;
  228.     while (paraOffset + curOffset < offset)
  229.         {
  230.         oldOffset = paraOffset + curOffset;
  231.         wrapText(p->text, p->size, curOffset, p->wrap);
  232.         ++line;
  233.         if (curOffset >= p->size)
  234.             {
  235.             paraOffset += p->size;
  236.             p = p->next;
  237.             curOffset = 0;
  238.             }
  239.         }
  240.     loc.x = offset - oldOffset - 1;
  241.     loc.y = line;
  242.     length = crossRefPtr->length;
  243.     ref = crossRefPtr->ref;
  244. }
  245.  
  246. char *THelpTopic::getLine( int line )
  247. {
  248.     int offset, i;
  249.     TParagraph *p;
  250.     char buffer[256];
  251.  
  252.     if (lastLine < line)
  253.         {
  254.         i = line;
  255.         line -= lastLine;
  256.         lastLine = i;
  257.         offset = lastOffset;
  258.         p = lastParagraph;
  259.         }
  260.     else
  261.         {
  262.         p = paragraphs;
  263.         offset = 0;
  264.         lastLine = line;
  265.         }
  266.     buffer[0] = 0;
  267.     while (p != 0)
  268.     {
  269.         while (offset < p->size)
  270.         {
  271.             --line;
  272.             strcpy(buffer, wrapText(p->text, p->size, offset, p->wrap));
  273.             if (line == 0)
  274.                 {
  275.                 lastOffset = offset;
  276.                 lastParagraph = p;
  277.                 return buffer;
  278.                 }
  279.         }
  280.         p = p->next;
  281.         offset = 0;
  282.     }
  283.     buffer[0] = 0;
  284.     return buffer;
  285. }
  286.  
  287. int THelpTopic::getNumCrossRefs()
  288. {
  289.     return numRefs;
  290. }
  291.  
  292. int THelpTopic::numLines()
  293. {
  294.     int offset, lines;
  295.     TParagraph *p;
  296.  
  297.     offset = 0;
  298.     lines = 0;
  299.     p = paragraphs;
  300.     while (p != 0)
  301.         {
  302.         offset = 0; 
  303.         while (offset < p->size)
  304.             {
  305.             ++lines;
  306.             wrapText(p->text, p->size, offset, p->wrap);
  307.             }
  308.         p = p->next;
  309.         }
  310.     return lines;
  311. }
  312.  
  313. void THelpTopic::setCrossRef( int i, TCrossRef& ref )
  314. {
  315.     TCrossRef *crossRefPtr;
  316.  
  317.     if (i < numRefs)
  318.         {
  319.         crossRefPtr = crossRefs + i;
  320.         *crossRefPtr = ref;
  321.         }
  322. }       
  323.  
  324.  
  325. void THelpTopic::setNumCrossRefs( int i )
  326. {
  327.     TCrossRef  *p, *crossRefPtr;
  328.  
  329.     if (numRefs == i)
  330.         return;
  331.     p = new TCrossRef[i];
  332.     if (numRefs > 0)
  333.         {
  334.         crossRefPtr = crossRefs;
  335.         if (i > numRefs)
  336.             memmove(p, crossRefPtr, numRefs * sizeof(TCrossRef));
  337.         else
  338.             memmove(p, crossRefPtr, i * sizeof(TCrossRef));
  339.  
  340.         delete [numRefs] crossRefPtr; 
  341.         }
  342.     crossRefs = p;
  343.     numRefs = i;
  344. }
  345.  
  346.  
  347. void THelpTopic::setWidth( int aWidth )
  348. {
  349.     width = aWidth;
  350. }
  351.  
  352. void THelpTopic::writeParagraphs( opstream& s )
  353. {
  354.     int i;
  355.     TParagraph  *p;
  356.     int temp;
  357.  
  358.     p = paragraphs;
  359.     for (i = 0; p != 0; ++i)
  360.         p = p->next;
  361.     s << i;
  362.     for(p = paragraphs; p != 0; p = p->next)
  363.         {
  364.         s << p->size;
  365.         temp = int(p->wrap);
  366.         s << temp;
  367.         s.writeBytes(p->tex